#ifndef __CMS_COMMON_KRFFVEAC
#define __CMS_COMMON_KRFFVEAC

#include <stdint.h>
#include <inttypes.h>
#include <errno.h>

/****************************************************************
 * DIR define
 * *************************************************************/
#define CMS_LOG_FILE                "/var/log/cmsLog"
#define CMS_LOG_FILE_BAK            CMS_LOG_FILE"2"
#define CMS_LOG_LEVEL_FILE          CUR_XML_PATH"/cmsLogLevel"

#define CMS_KEY_DIR                 "/tmp/"
#define CMS_MODULE_FILE_PATH        CUR_XML_PATH

#define CMS_DEF_FILE_ENC            DEF_XML_PATH"/"DEF_CONF_FILE_ENC
#define CMS_CUR_FILE_XML            CUR_XML_PATH"/"CUR_CONF_FILE_XML
#define CMS_CUR_FILE_ENC            CUR_XML_PATH"/"CUR_CONF_FILE_ENC
#define CMS_PRE_FILE_SHM            DEF_XML_PATH"/"PRE_CONF_FILE_SHM
#define CMS_CUR_ENC_CRC             CUR_XML_PATH"/configCRC"
#define CMS_CUR_ENC_CRC_CHECK       "/tmp/configCRC_check"

/**************************************************************/
//#define CMS_SUPPORT_YL


typedef unsigned long long cmsuint64;
typedef unsigned int cmsuint32;
typedef unsigned short cmsuint16;
typedef unsigned char cmsuint8;
typedef unsigned char cmsBoolean;
typedef signed char cmssint7;
typedef signed short cmssint15;
typedef signed int cmssint31;
typedef signed long long cmssint63;
typedef uint32_t	cmsoffset_t;		

/*Definitions for parameter type*/
#define PARAMETER_TYPE_UINT8	1
#define PARAMETER_WRITE_UINT8   PARAMETER_TYPE_UINT8
#define PARAMETER_TYPE_UINT16	2
#define PARAMETER_WRITE_UINT16  PARAMETER_TYPE_UINT16
#define PARAMETER_TYPE_UINT32	4
#define PARAMETER_WRITE_UINT32  PARAMETER_TYPE_UINT32
#define PARAMETER_TYPE_MAC	6
#define PARAMETER_WRITE_MAC  PARAMETER_TYPE_MAC
#define PARAMETER_TYPE_UINT64	8
#define PARAMETER_WRITE_UINT64  PARAMETER_TYPE_UINT64
#define PARAMETER_TYPE_IPV6       16
#define PARAMETER_WRITE_IPV6  PARAMETER_TYPE_IPV6 
#define PARAMETER_TYPE_DATETIME   27
#define PARAMETER_WRITE_DATETIME    PARAMETER_TYPE_DATETIME
#define PARAMETER_RESTORE		(1 << 28)
#define PARAMETER_TYPE_EXTENSION  (1 << 29)
#define PARAMETER_WRITE_EXTENSION PARAMETER_TYPE_EXTENSION
#define PARAMETER_TYPE_BOOLEAN    (PARAMETER_TYPE_EXTENSION | 1)
#define PARAMETER_WRITE_BOOLEAN PARAMETER_TYPE_BOOLEAN
#define PARAMETER_TYPE_IPV4       (PARAMETER_TYPE_EXTENSION | 4)
#define PARAMETER_WRITE_IPV4    PARAMETER_TYPE_IPV4
#define PARAMETER_TYPE_SINT7    ((1 << 31) + PARAMETER_TYPE_UINT8)
#define PARAMETER_WRITE_SINT7   PARAMETER_TYPE_SINT7
#define PARAMETER_TYPE_SINT15   ((1 << 31) + PARAMETER_TYPE_UINT16)
#define PARAMETER_WRITE_SINT15  PARAMETER_TYPE_SINT15
#define PARAMETER_TYPE_SINT31   ((1 << 31) + PARAMETER_TYPE_UINT32)
#define PARAMETER_WRITE_SINT31  PARAMETER_TYPE_SINT31
#define PARAMETER_TYPE_SINT63   ((1 << 31) + PARAMETER_TYPE_UINT64)
#define PARAMETER_WRITE_SINT63  PARAMETER_TYPE_SINT63
#define PARAMETER_TYPE_STRING   ((1 << 31) + (1 << 30))
#define PARAMETER_WRITE_STRING  PARAMETER_TYPE_STRING
#define PARAMETER_TYPE_CLASS_BITS ((1 << 31) | (1 << 30) | (1 << 29) | (1 << 28))

#define CMSG_DEBUG_FLAG 0
#define MAC_LENGTH	6

#define CMS_OK			0
#define CMS_NOK			-1
#define CMS_POK			-2
#define CMS_SENDOK		-3	/*send shm with data success*/
#define CMS_RECVOTHER	-4	/*recv msg response, but not corresponding*/
#define CMS_RESPONSEFAIL -5    /*recv msg response result is fail*/
#define CMS_NOCHANGE  	1	/*set same parameter value*/
#define CMS_OUTOFRANGE 	2	/*the parameter value is out of range*/

#define CMS_MSG_TYPE	0x11	/*define for cms stats*/

#define GC_WLAN_MAC_MASK_1       0x33
#define GC_WLAN_MAC_MASK_2       0xc4
#define GC_WLAN_MAC_MASK_3       0x88
#define GC_WLAN_MAC_MASK_UNICAST 0xfe
#define GC_WLAN_MAC_MASK_LOCAL   0x2

/*define for cmsboolean*/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

/*define for uint64 & int64  printf*/
#if __WORDSIZE == 64
#define U64_	"%lu"
#define S64_	"%ld"
#define LONG_          "%ld"
#else
#define U64_	"%llu"
#define S64_	"%lld"
#define LONG_           "%ld"
#endif

/*module name*/
#define CMS_CORE 	"cms_core"
#define CMS_TASK 	"cms_task"
#define CMS_DB		"cms_db"
#define CMS_SAMPLE 	"cms_sample"
#define CMS_EVENT 	"cms_event"
#define CMS_CGI     "cms_cgi"
#define CMS_HTTP    "cms_minihttp"
#define CMS_CWMP	"cms_cwmp"
#define CMS_OAM     "cms_oam"
#define CMS_DBUS	"cms_dbus"
#define CMS_WLAN_EVENT_AGENT 	"cms_wlan_event_agent"
#define CMS_EVENT_AGENT 		"cms_event_agent"
#define CMS_LOOPDETECT 			"gcloopdetect"
#define CMS_DNSMASQ 			"dnsmasq"

#define CMS_KEY_PID_MASK 		0x00ffff
#define CMS_KEY_MQ_PREFIX		0x010000
#define CMS_KEY_EQ_PREFIX		0x020000
#define CMS_KEY_HTTPD_PREFIX	0x200000
#define CMS_KEY_OTHERS_PREFIX   0xe00000


#define CMS_COMMON_MAX_CONNECTION	32

enum {
	CMS_EC_BASE = 1000,
	/*******************************
	 * error code for common CMS   *
	 *******************************/

	/*
	 * this tends to replace CMS_NOK for uncertain cause of failure or
	 * no suitable error code
	 */
	CMS_EC_COMMON_ERROR                   = CMS_EC_BASE + 0,
	/* fail to fetch share memory */
	CMS_EC_SHM_ERROR                      = CMS_EC_BASE + 1,
	/* exceed max number of node when access {i} node */
	CMS_EC_EXCEED_MAX_MODE                = CMS_EC_BASE + 2,
	/* fail to get instance of certian object node */
	CMS_EC_OBJ_ERROR                      = CMS_EC_BASE + 3,
	/* fail to get/query an valid index of instance of certian object node */
	CMS_EC_OBJ_IDX_ERROR                  = CMS_EC_BASE + 4,

	CMS_EC_COMMON_END                     = CMS_EC_BASE + 99,

	/*******************************
	 * error code for WLAN modeule *
	 *******************************/
	CMS_EC_WLAN_BASE                      = CMS_EC_BASE + 100,
	/* primary interface requried */
	CMS_EC_WLAN_PRIMARY_IF                = CMS_EC_WLAN_BASE + 1,
	/* wlan API not supported */
	CMS_EC_WLAN_API_UNSUPPORT             = CMS_EC_WLAN_BASE + 2,
	/* invalid name of a wlan network interface */
	CMS_EC_WLAN_INVALID_IFNAME            = CMS_EC_WLAN_BASE + 3,
	/* fail to get an valid WLAN index */
	CMS_EC_WLAN_INVALID_IF_IDX            = CMS_EC_WLAN_BASE + 4,
	/* invalid parameter value */
	CMS_EC_WLAN_INVALID_PARAM             = CMS_EC_WLAN_BASE + 5,
	/* wlan init error */
	CMS_EC_WLAN_INIT_ERROR                = CMS_EC_WLAN_BASE + 6,
	/* wlan if not up */
	CMS_EC_WLAN_IF_NOT_UP				  = CMS_EC_WLAN_BASE + 7,

	CMS_EC_WLAN_END                       = CMS_EC_WLAN_BASE + 99,
};

/*******************************
 * for wlan *
 *******************************/
/* to get IA of depth 1 node, let's use cmsXmlSetIAValue() instead of cmsXmlConstructIAValue() */
#define ONE_DEPTH_IA                  1

#define PSK_HEX_SIZE                 64
#define SSID_NUM_24_G                 8
#define SSID_NUM_5_G                  8

#ifndef BIT
#define BIT(x) (1 << (x))
#endif

#define SSID_BITMAP_TR69          BIT(0)
#define SSID_BITMAP_OSGI          BIT(1)
#define SSID_BITMAP_USER_GUI      BIT(2)
#define SSID_BITMAP_ROOT_GUI      BIT(3)
#define SSID_BITMAP_SHARED        BIT(16)
#define SSID_BITMAP_GUEST         BIT(17)
#define SSID_BITMAP_SAME_SSID     BIT(18)
#define SSID_BITMAP_MESH_SSID     BIT(19)
#define SSID_BITMAP_BACKUP_SSID   BIT(20)
#define SSID_BITMAP_AIR_ENGV1     BIT(31)


#ifdef GC_FPGA
#define LAN1 "eth1.0"
#else
#define LAN1 "eth0.0"
#endif



#ifdef GC_APPS_CU
#define SERVICELIST_VOIP  "VOICE"
#elif defined(GC_APPS_CTC)
#define SERVICELIST_VOIP  "VOIP"
#elif defined(GC_APPS_CMCC)
#define    SERVICELIST_VOIP  "VOIP"
#else
#define SERVICELIST_VOIP  "VOIP"
#endif


#if defined(GC_SUPPORT_ROUTER_FTTR) || defined(GC_APPS_CTC)
	#define ROUTER_MODE_BRIDGE 0
	#define ROUTER_MODE_ROUTER 1
	#define ROUTER_MODE_REPEAT 2
#endif


/*define for back partition & pre partition*/
#define CMS_BACKUP_CONFIG_FILE "/tmp/config.curBak.xml"
#define CMS_PRE_CONFIG_FILE	"/tmp/config.preDownload.xml"

#define AOSNET_MANAGEPLUGIN_FILE "/tmp/aosnetMPluginDone"

#define INSERT_NODE(pHead, pNode){\
	pNode->prev = NULL;\
	pNode->next = NULL;\
	if(pHead == NULL)\
		pHead = pNode;\
	else{\
		pNode->next = pHead->next;\
		pNode->prev = pHead;\
		if(pHead->next != NULL){\
			pHead->next->prev = pNode;\
		}\
		pHead->next = pNode;\
	}\
}

#define INSERT_NODE_BY_PRIORITY(pHead, pNode){\
	pNode->prev = NULL;\
	pNode->next = NULL;\
	if(!pHead){\
		pHead = pNode;\
	}else{\
		pNode->next = pHead;\
		while((pNode->next) && (pNode->next->priority <= pNode->priority)){\
			pNode->prev = pNode->next;\
			pNode->next = pNode->next->next;\
		}\
		if(pNode->next)\
			pNode->next->prev = pNode;\
		if(pNode->prev)\
			pNode->prev->next = pNode;\
		else\
			pHead = pNode;\
	}\
}

#define DELETE_NODE(pHead, pNode){\
	if(pNode->next)\
		pNode->next->prev = pNode->prev;\
	if(pNode->prev)\
		pNode->prev->next = pNode->next;\
	else\
		pHead = pNode->next;\
	free(pNode);\
	pNode = NULL;\
}

#define SWITCH_NODE(ori, new, head){\
	if(ori == head) head == new;\
	if(ori->prev) ori->prev->next = new;\
	if(ori->next) ori->next->prev = new;\
	new->prev = ori->prev;\
	new->next = ori->next;\
	new = ori;\
} 


#define safe_free(p){\
	if(p != NULL){\
		free(p);\
		p = NULL;\
	}\
}

typedef struct cms_common_initInfo_s
{
	int fxsNumber;
	int lanNumber;
	int wlanSupport;
	int usbNumber;
	int simcardSupport;
	int wlan5GSupport;
	unsigned char wanPhisicalType[16];
}cms_common_initInfo_t;


typedef struct cms_common_wanInfo_s
{
	int enable;
	int index;
	int iWDev;
	int iWCDev;
	int iIPEntry;
	int iPPPEntry;
	char ifname[65];
	char l2ifname[20];
	char baseIfName[20];
	char serviceType[46];
	char addressType[14];
	char connectionType[16];
	char PhisicalType[16];
	char connStatus[32];
	char LanInterface[1024];
	int vlanID;
	int vlanPID;
	int mcastVlan;
	char ExternalIPAddress[32];
	char subMask[32];
	char defaultGw[32];
	int dhcpEnable;
	int ipMode;
	char IPv6IPAddressOrigin[32];
	char IPv6IPGetMethodOrigin[32];
	int dsliteEnable;
	unsigned char macAddr[6];
	unsigned char EnableDSCPMark;
	unsigned char DSCPMarkValue;
	int natEnabled;
	int mtu;
	int dhcpForward;
	int passThrough;
	char username[32];
	char password[32];
	int proxyEnable;
	int maxUser;
	int multicastVlan;
	int TotalBytesSent;
	int TotalBytesReceived;
	int TotalpacketsSent;
	int TotalPacketsReceived;
}cms_common_wanInfo_t;


typedef struct cms_common_content_s
{
	cms_common_initInfo_t initInfo;
	int ipForwardModeUsed;
	cms_common_wanInfo_t wanInfo[CMS_COMMON_MAX_CONNECTION];

}cms_common_content_t;




int cms_Common_Init(cms_common_content_t **content, cms_common_initInfo_t *initInfo);

int cms_do_cmd(const char *cmd);

#endif /* end of include guard: __CMS_COMMON_KRFFVEAC */
